﻿using AndoridMoeTool.AndroidTool.SDK.Media;
using CatUtils.Utils.Currency.IoUtils;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;

namespace CatUtils.Utils.SpecialUtils.VerificationCode
{
    /// <summary>
    /// 验证码识别demo
    /// </summary>
    public class VerificationImgClear
    {
        /// <summary>
        /// 处理图像
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public string HandleImg(string FilePath)
        {
            string TempPath = FileNameHelper.CreateTempFileName();
            System.Console.WriteLine(TempPath);
            FileHelper.FileCopy(FilePath, TempPath, true);
            TempPath = new MagickImageHelper().ImgThreshold(TempPath, 75, "");//请根据图片调试二值化参数
            TempPath = new MagickImageHelper().ImgGray(TempPath, "");
            TempPath = new MagickImageHelper().ImgNegateGray(TempPath, false, "");     //启用反转灰化模式

            return TempPath;
        }

#pragma warning disable S1144 // Unused private types or members should be removed

        /// <summary>
        /// 膨胀算法
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        private Bitmap GetPengZhangImage(Bitmap image)
        {
            List<Point> setList = new List<Point>();

            try
            {
                image = image.Clone(new Rectangle(0, 0, image.Width, image.Height), PixelFormat.Format24bppRgb);
                for (int i = 0; i < image.Width; i++)
                {
                    for (int j = 0; j < image.Height; j++)
                    {
                        // 如果应该设置为黑色的
                        if (SetPixelPengZhang(image, i, j))
                        {
                            setList.Add(new Point(i, j));
                        }
                    }
                }

                int x, y;
                foreach (var item in setList)
                {
                    x = item.X;
                    y = item.Y;

                    image.SetPixel(x - 1, y, Color.Black);
                    image.SetPixel(x, y - 1, Color.Black);
                }
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(ex);
            }

            return image;
        }

#pragma warning restore S1144 // Unused private types or members should be removed
        /// <summary>
        /// 判断这个点应不应该设置为黑色
        /// </summary>
        /// <param name="image"></param>
        /// <param name="i"></param>
        /// <param name="j"></param>
        /// <returns></returns>

        protected bool SetPixelPengZhang(Bitmap image, int i, int j)
        {
            if (i != 0 && j != 0)
            {
                if (image.GetPixel(i, j).ToArgb() == Color.Black.ToArgb())
                {
                    return true;
                }
                else return false;
            }
            else return false;
        }

#pragma warning disable S1144 // Unused private types or members should be removed

        /// <summary>
        /// 腐蚀（来消除小且无意义的物体）
        /// </summary>
        /// <param name="image">二值化图片</param>
        /// <returns></returns>
        private Bitmap GetFuShiImage(Bitmap image)
        {
            List<Point> setList = new List<Point>();

            image = image.Clone(new Rectangle(0, 0, image.Width, image.Height), PixelFormat.Format24bppRgb);

            for (int i = 0; i < image.Width; i++)
            {
                for (int j = 0; j < image.Height; j++)
                {
                    // 如果是应该设置为黑色的点，将其添加到我们要设的list中
                    if (SetPixelFuShi(image, i, j))
                    {
                        setList.Add(new Point(i, j));
                    }
                }
            }

            // 遍历，设置相应的值
            foreach (var item in setList)
            {
                image.SetPixel(item.X, item.Y, Color.White);
            }

            return image;
        }

#pragma warning restore S1144 // Unused private types or members should be removed

        /// <summary>
        /// 判断一个点是不是应该设置为白颜色
        /// </summary>
        /// <param name="image"></param>
        /// <param name="i"></param>
        /// <param name="j"></param>
        /// <returns></returns>
        protected bool SetPixelFuShi(Bitmap image, int i, int j)
        {
            int origion;
            int upPoint;
            int leftPoint;

            if (i != 0 && j != 0)
            {
                origion = image.GetPixel(i, j).ToArgb();
                upPoint = image.GetPixel(i, j - 1).ToArgb();
                leftPoint = image.GetPixel(i - 1, j).ToArgb();

                if (origion == Color.Black.ToArgb() &&
                   upPoint == Color.Black.ToArgb() &&
                   leftPoint == Color.Black.ToArgb())
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else return true;
        }
    }
}